using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Services;
using System.Diagnostics;

namespace Sudoku
{
	public class ExtendedGameComponent : DrawableGameComponent
	{
		protected SpriteBatch spriteBatch;
		protected List<ExtendedGameComponent> components;
		protected TextureLibrary textureService;
		protected FontLibrary fontService;
		protected SoundLibrary soundService;

		public ExtendedGameComponent(Game game)
			: base(game)
		{
			components = new List<ExtendedGameComponent>();

			textureService = Game.Services.GetService(typeof(TextureLibrary)) as TextureLibrary;
			fontService = Game.Services.GetService(typeof(FontLibrary)) as FontLibrary;
			soundService = Game.Services.GetService(typeof(SoundLibrary)) as SoundLibrary;
		}

		public override void Initialize()
		{
			foreach (ExtendedGameComponent child in components)
				child.Initialize();

			base.Initialize();
		}

		protected override void LoadContent()
		{
			spriteBatch = new SpriteBatch(GraphicsDevice);

			foreach (ExtendedGameComponent child in components)
				child.LoadContent();

			base.LoadContent();
		}

		public override void Draw(GameTime gameTime)
		{
			foreach (ExtendedGameComponent child in components)
				child.Draw(gameTime);

			base.Draw(gameTime);
		}

		public override void Update(GameTime gameTime)
		{
			if (Enabled)
			{
				foreach (ExtendedGameComponent child in components)
					child.Update(gameTime);
			}
			
			base.Update(gameTime);			
		}

		protected override void Dispose(bool disposing)
		{
			foreach (ExtendedGameComponent child in components)
				child.Dispose(); 
			
			base.Dispose(disposing);
		}

		public List<ExtendedGameComponent> Components
		{
			get { return components; }
		}

		protected override void OnEnabledChanged(object sender, EventArgs args)
		{
			base.OnEnabledChanged(sender, args);

			//if (components.Count > 0) Debug.WriteLine(components.Count);
			foreach (ExtendedGameComponent child in components)
				child.Enabled = this.Enabled;
		}
		
	}
}
